<?php
/**
 * PermissionRepo.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-07-11 20:49:45
 * @modified   2023-07-11 20:49:45
 */

namespace Beike\Seller\Repositories;

use Beike\Seller\Models\SellerUser;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;

class PermissionRepo
{
    private ?SellerUser $sellerUser = null;

    public function setUser($user): self
    {
        $this->sellerUser = $user;

        return $this;
    }

    /**
     * 所有权限列表
     *
     * @return array
     * @throws \Exception
     */
    public function getPermissions(): array
    {
        $permissions = [
            [
                'title'       => trans('seller::product.title'),
                'permissions' => $this->getPermissionList('products', ['index', 'show', 'edit', 'create', 'delete']),
            ],
            [
                'title'       => trans('seller::category.title'),
                'permissions' => $this->getPermissionList('categories', ['index', 'edit', 'create', 'delete']),
            ],
            [
                'title'       => trans('seller::order.title'),
                'permissions' => $this->getPermissionList('orders', ['index', 'show', 'edit', 'export']),
            ],
            [
                'title'       => trans('seller::shipping.title'),
                'permissions' => $this->getPermissionList('shipping', ['index', 'show', 'edit', 'create', 'delete']),
            ],
            [
                'title'       => trans('seller::user.title'),
                'permissions' => $this->getPermissionList('users', ['index', 'show', 'edit', 'create', 'delete']),
            ],
            [
                'title'       => trans('seller::transaction.title'),
                'permissions' => $this->getPermissionList('transactions', ['index']),
            ],
            [
                'title'       => trans('seller::withdraw.title'),
                'permissions' => $this->getPermissionList('withdraws', ['index', 'create']),
            ],
            [
                'title'       => trans('seller::rma.title'),
                'permissions' => $this->getPermissionList('rmas', ['index', 'show', 'edit']),
            ],
            [
                'title'       => trans('seller::file_manager.title'),
                'permissions' => $this->getPermissionList('file_managers', ['read', 'write']),
            ],
        ];

        return $permissions;
    }

    private function getPermissionList($module, $actions)
    {
        $permissions = [];
        foreach ($actions as $action) {
            $code          = "{$module}_{$action}";
            $permissions[] = [
                'code'     => $code,
                'name'     => trans("seller::common.permission.$action"),
                'selected' => $this->hasPermission($code),
            ];
        }

        return $permissions;
    }

    /**
     * 判断当前用户是否有权限
     *
     * @param $permission
     * @return bool
     */
    private function hasPermission($permission): bool
    {
        try {
            if ($this->sellerUser) {
                return $this->sellerUser->hasPermissionTo($permission);
            }
        } catch (PermissionDoesNotExist $exception) {
            return false;
        }

        return false;
    }
}
